﻿using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/**
挂载在button下,删除原来的button组件
**/
namespace VFramework
{
    /**
	 * VUIButton 按钮封装类
	 * 比原来的按钮多了很多功能
	 * 
	 * 比如长按事件，拖拽事件，开始点击，结束点击
	 * click()可以判断释放点击是不是在按钮区域
	**/
    public class VUIButton : Selectable, IPointerDownHandler, IPointerExitHandler, IPointerUpHandler, IDragHandler
    {


        float interval = 0.1f;
        float longPressDelay = 0.5f;

        public int index = 0;

        private bool isTouchDown = false;
        private bool isLongpress = false;
        private bool isClick = false;

        private float touchBegin = 0;
        private float lastInvokeTime = 0;

        public delegate void EHandler(VUIButton sender);

        public delegate void EHandler2(VUIButton sender, PointerEventData eventData);



        public event EHandler onLongPress = null;

        public event EHandler2 onTouchBegan = null;
        public event EHandler2 onTouchMove = null;
        public event EHandler2 onTouchEnd = null;


        // Update is called once per frame
        void Update()
        {
            if (isTouchDown && IsPressed() && interactable)
            {
                if (isLongpress)
                {
                    if (Time.time - lastInvokeTime > interval)
                    {
                        if (onLongPress != null)
                            onLongPress.Invoke(this);
                        lastInvokeTime = Time.time;
                    }
                }
                else
                {
                    isLongpress = Time.time - touchBegin > longPressDelay;
                }
            }
        }

        public bool click()
        {
            return this.isClick;
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (interactable == false)
                return;

            if (onTouchMove != null)
            {
                onTouchMove.Invoke(this, eventData);
            }


        }

        public override void OnPointerDown(PointerEventData eventData)
        {


            isClick = false;
            if (interactable == false)
                return;

            base.OnPointerDown(eventData);

            if (onTouchBegan != null)
            {
                onTouchBegan.Invoke(this, eventData);
            }



            touchBegin = Time.time;
            isTouchDown = true;
        }



        public override void OnPointerExit(PointerEventData eventData)
        {
            if (interactable == false)
                return;

            base.OnPointerExit(eventData);
            isTouchDown = false;
            isLongpress = false;
        }

        public override void OnPointerUp(PointerEventData eventData)
        {

			isClick = false;
			isTouchDown = false;
			isLongpress = false;

            if (interactable == false)
                return;

            base.OnPointerUp(eventData);

            Vector2 lp;
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out lp))
            {

                RectTransform rt = GetComponent<RectTransform>();
                Vector2 size = new Vector2(rt.sizeDelta.x * rt.localScale.x, rt.sizeDelta.y * rt.localScale.y);
                Vector2 minPos = new Vector2(-size.x * rt.pivot.x, -size.y * rt.pivot.y);
                Vector2 maxPos = new Vector2(size.x * (1 - rt.pivot.x), size.y * (1 - rt.pivot.y));
                if (lp.x >= minPos.x && lp.x <= maxPos.x && lp.y >= minPos.y && lp.y <= maxPos.y)
                {
                    isClick = true;
                }
            }
            else
            {

            }

			if (onTouchEnd != null) {
                onTouchEnd.Invoke(this, eventData);
			}

            
        }
    }
}